package service

import (
	"context"
	"fmt"
	"user-srv/handler/dao"

	__ "user-srv/basic/proto"
)

// 信任设备添加
func (s *Server) TrustedDeviceAdd(_ context.Context, in *__.TrustedDeviceAddReq) (*__.TrustedDeviceAddResp, error) {
	if in.DeviceType == "" || in.DeviceName == "" || in.OsType == "" {
		return nil, fmt.Errorf("参数不能为空")
	}

	add, err := dao.TrustedDeviceAdd(in)
	if err != nil {
		return nil, fmt.Errorf(err.Error())
	}

	return &__.TrustedDeviceAddResp{
		Id: int64(add.Id),
	}, nil
}

// 信任设备删除
func (s *Server) TrustedDeviceDel(_ context.Context, in *__.TrustedDeviceDelReq) (*__.TrustedDeviceDelResp, error) {
	if in.Id == 0 {
		return nil, fmt.Errorf("参数错误")
	}

	_, err := dao.TrustedDeviceDel(in)
	if err != nil {
		return nil, fmt.Errorf(err.Error())
	}

	return &__.TrustedDeviceDelResp{}, nil
}

// 信任设备修改
func (s *Server) TrustedDeviceUpdate(_ context.Context, in *__.TrustedDeviceUpdateReq) (*__.TrustedDeviceUpdateResp, error) {

	update, err := dao.TrustedDeviceUpdate(in)
	if err != nil {
		return nil, fmt.Errorf(err.Error())
	}

	return &__.TrustedDeviceUpdateResp{
		Id: int64(update.Id),
	}, nil
}

// 信任设备展示
func (s *Server) TrustedDeviceFind(_ context.Context, in *__.TrustedDeviceFindReq) (*__.TrustedDeviceFindResp, error) {

	find, err := dao.TrustedDeviceFind(in)
	if err != nil {
		return nil, fmt.Errorf(err.Error())
	}

	return &__.TrustedDeviceFindResp{
		List: find,
	}, nil
}
